<?php

define('FDMS_WSDL', "http://soap.aldorservices.com/aldorsolutions01.wsdl" );
define('FDMS_PASSWORD', "10key10");

class FDMS_API_calls
{	
	// modification - this needs to return an associative array with the main key
	// being the FDMS funeral home id and the carried data being an associative
	// array of the whole thing.
    public static function listFuneralHomes()
    {
        $results = array();
        
		try {
        	$soapClient = new SoapClient( FDMS_WSDL );
		}
		catch(Exception $e)
		{
			echo "exception: $e";	
		}
		
        if(null == $soapClient)
        {
            $results['status'] = "FAIL";
            $results['reason'] = "Cannot instantiate SOAP client";
        }
        else
        {
            $rawxml = $soapClient->ListFuneralHomesSelect( FDMS_PASSWORD );

            // take the payload out of the SOAP envelope
            $idx1 = stripos( $rawxml, "<data_structure>" );
            $idx2 = stripos( $rawxml, "</data_structure>" );
            if($idx1 >= $idx2)
            {
                $results['status'] = "FAIL";
                $results['reason'] = "Malformed response (or response has been altered).";
            }
            else
            {
                $payload = substr( $rawxml, $idx1, $idx2+strlen("</data_structure>")-$idx1);
                $payloadxml = simplexml_load_string($payload);
                if(!$payloadxml)
                {
                    $results['status'] = "FAIL";
                    $results['reason'] = "Malformed response (or response has been altered).";
                }
                else
                {
                    $rows = array();
                    // and loop thru the homes returned, building an array of associative arrays
                    $datarows = $payloadxml->children();
                    foreach( $datarows as $datarow )
                    {
                        $row['id'] = "".$datarow->FuneralHomeId;
                        $row['name'] = "".$datarow->FuneralHomeName;
                        $row['address'] = "".$datarow->FuneralHomeAddress;
                        $row['city'] = "".$datarow->FuneralHomeCity;
                        $row['state'] = "".$datarow->FuneralHomeState;
                        $row['zip'] = "".$datarow->FuneralHomeZip;
                        $row['phone'] = "".$datarow->FuneralHomePhone;

                        $rows[] = $row;
                    }

                    $results['rows'] = $rows;
                }
            }
        }

        return $results;
    }
	
    public static function getFuneralHomeDetail($fhid)
    {
        $results = array();

        $soapClient = new SoapClient( FDMS_WSDL );
        if(null == $soapClient)
        {
            $results['status'] = "FAIL";
            $results['reason'] = "Cannot instantiate SOAP client";
        }
        else
        {
            $rawxml = $soapClient->ListFuneralHomeDetailSelect( $fhid, FDMS_PASSWORD );
            
            // take the payload out of the SOAP envelope
            $idx1 = stripos( $rawxml, "<data_structure>" );
            $idx2 = stripos( $rawxml, "</data_structure>" );
            if($idx1 >= $idx2)
            {
                $results['status'] = "FAIL";
                $results['reason'] = "Malformed response (or response has been altered).";
            }
            else
            {
                $payload = substr( $rawxml, $idx1, $idx2+strlen("</data_structure>")-$idx1);
                $payloadxml = simplexml_load_string($payload);
                if(!$payloadxml)
                {
                    $results['status'] = "FAIL";
                    $results['reason'] = "Malformed response (or response has been altered).";
                }
                else
                {
                    $rows = array();
                    // and loop thru the homes returned, building an array of associative arrays
                    $datarows = $payloadxml->children();
                    foreach( $datarows as $datarow )
                    {
                        $row['id'] = "".$datarow->FuneralHomeId;
                        $row['name'] = "".$datarow->FuneralHomeName;
                        $row['address'] = "".$datarow->FuneralHomeAddress;
                        $row['city'] = "".$datarow->FuneralHomeCity;
                        $row['state'] = "".$datarow->FuneralHomeState;
                        $row['zip'] = "".$datarow->FuneralHomeZip;
                        $row['phone'] = "".$datarow->FuneralHomePhone;
                        $row['email'] = "".$datarow->FuneralHomeEmail;
                        $row['website'] = "".$datarow->FuneralHomeWebsite;
                        $row['homenum'] = "".$datarow->FuneralHomeNum;
                        $rows[] = $row;
                    }

                    $results['rows'] = $rows;
                }
            }
        }

        return $results;
    }

//ListFuneralDirectorsSelect
    public static function listFuneralDirectors( $homeID )
    {
        $results = array();
        
        $soapClient = new SoapClient( FDMS_WSDL );
        if(null == $soapClient)
        {
            $results['status'] = "FAIL";
            $results['reason'] = "Cannot instantiate SOAP client";
        }
        else
        {
            $rawxml = $soapClient->ListLocationsFuneralDirectorsSelect( $homeID, FDMS_PASSWORD );

            // take the payload out of the SOAP envelope
            $idx1 = stripos( $rawxml, "<data_structure>" );
            $idx2 = stripos( $rawxml, "</data_structure>" );
            if($idx1 >= $idx2)
            {
                $results['status'] = "FAIL";
                $results['reason'] = "Malformed response (or response has been altered).";
            }
            else
            {
                $payload = substr( $rawxml, $idx1, $idx2+strlen("</data_structure>")-$idx1);
                $payloadxml = simplexml_load_string($payload);
                if(!$payloadxml)
                {
                    $results['status'] = "FAIL";
                    $results['reason'] = "Malformed response (or response has been altered).";
                }
                else
                {
                    $rows = array();
                    // and loop thru the homes returned, building an array of associative arrays
                    $datarows = $payloadxml->children();
                    foreach( $datarows as $datarow )
                    {
                        $row['id'] = "".$datarow->ArrangerId;
                        $row['name'] = "".$datarow->FuneralDirectorFullName;
						$row['funeral_home_name'] = "".$datarow->FuneralDirectorLocationName;
                        $row['funeral_home_id'] = "".$datarow->FuneralDirectorLoactionId; //sic

                        $rows[] = $row;
                    }

                    $results['rows'] = $rows;
                }
            }
        }

        return $results;
    }
	
    public static function listFuneralCasesForDirector( $_directorId )
    {
        $results = array();
        
        $soapClient = new SoapClient( FDMS_WSDL );
        if(null == $soapClient)
        {
            $results['status'] = "FAIL";
            $results['reason'] = "Cannot instantiate SOAP client";
        }
        else
        {
            $rawxml = $soapClient->ListCasesSelectByDirector( $_directorId, FDMS_PASSWORD );
			
            // take the payload out of the SOAP envelope
            $idx1 = stripos( $rawxml, "<data_structure>" );
            $idx2 = stripos( $rawxml, "</data_structure>" );
            if($idx1 >= $idx2)
            {
                $results['status'] = "FAIL";
                $results['reason'] = "Malformed response (or response has been altered).";
            }
            else
            {
                $payload = substr( $rawxml, $idx1, $idx2+strlen("</data_structure>")-$idx1);
                $payloadxml = simplexml_load_string($payload);
                if(!$payloadxml)
                {
                    $results['status'] = "FAIL";
                    $results['reason'] = "Malformed response (or response has been altered).";
                }
                else
                {
                    $rows = array();
                    // and loop thru the homes returned, building an array of associative arrays
                    $datarows = $payloadxml->children();
                    foreach( $datarows as $datarow )
                    {
                    	$row['funeral_director_id'] = "".$datarow->DecedentArrangerId;
                        $row['id'] = "".$datarow->DecedentCaseNumber;						
						$row['decedent_fname'] = "".$datarow->DecedentFirstName;
						$row['decedent_mname'] = "".$datarow->DecedentMiddleName;
						$row['decedent_lname'] = "".$datarow->DecedentLastName;
						$row['decedent_dob'] = "".$datarow->DecedentDob;
						$row['decedent_dod'] = "".$datarow->DecedentDod;
						$row['decedent_city'] = "".$datarow->DecedentResidentCity;
						$row['decedent_state'] = "".$datarow->DecedentResidentState;
						$row['case_status'] = "".$datarow->DecedentCaseStatus;
						$row['vitals_id'] = "".$datarow->DecedentVitalsId;						
                        $rows[] = $row;
                    }

                    $results['rows'] = $rows;
                }
            }
        }

        return $results;
    }
 
 	public static function getCaseDetails($vitalsId)
    {
        $results = array();

        $soapClient = new SoapClient( FDMS_WSDL );
        if(null == $soapClient)
        {
            $results['status'] = "FAIL";
            $results['reason'] = "Cannot instantiate SOAP client";
        }
        else
        {
            $rawxml = $soapClient->ListCaseSelect( $vitalsId, FDMS_PASSWORD );
			
            // take the payload out of the SOAP envelope
            $idx1 = stripos( $rawxml, "<data_structure>" );
            $idx2 = stripos( $rawxml, "</data_structure>" );
            if($idx1 >= $idx2)
            {
                $results['status'] = "FAIL";
                $results['reason'] = "Malformed response (or response has been altered).";
            }
            else
            {
                $payload = substr( $rawxml, $idx1, $idx2+strlen("</data_structure>")-$idx1);
                $payloadxml = simplexml_load_string($payload);
                if(!$payloadxml)
                {
                    $results['status'] = "FAIL";
                    $results['reason'] = "Malformed response (or response has been altered).";
                }
                else
                {
                    $rows = array();
                    // and loop thru the homes returned, building an array of associative arrays
                    $datarows = $payloadxml->children();
                    foreach( $datarows as $datarow )
                    {
                    	$row['funeral_director_id'] = "".$datarow->DecedentArrangerId;
                        $row['id'] = "".$datarow->DecedentCaseNumber;						
						$row['decedent_fname'] = "".$datarow->DecedentFirstName;
						$row['decedent_mname'] = "".$datarow->DecedentMiddleName;
						$row['decedent_lname'] = "".$datarow->DecedentLastName;
						$row['decedent_dob'] = "".$datarow->DecedentDob;
						$row['decedent_dod'] = "".$datarow->DecedentDod;
						$row['decedent_city'] = "".$datarow->DecedentResidentCity;
						$row['decedent_state'] = "".$datarow->DecedentResidentState;
						$row['case_status'] = "".$datarow->DecedentCaseStatus;
						$row['sale_type'] = "".$datarow->DecendentSaleType;
						$row['sale_type_code'] = "".$datarow->DecedentSaleTypeCode;
						$row['service_type'] = "".$datarow->DecedentServiceType;
						$row['vitals_id'] = "".$datarow->DecedentVitalsId;						
                        $rows[] = $row;
                    }

                    $results['rows'] = $rows;
                }
            }
        }

        return $results;
    }

    }
?>
